package gui.visu.listeners;

import gui.visu.components.popupdisplaydata.A_PopupDisplayData;

import java.awt.event.MouseEvent;

import controls.visu.BandControl;
import data.visu.structures.Range;
import data.visu.structures.ValueAndString;

/**
 * @author Charles Perin
 * 29 mars 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 *
 */
public class ListenerPopupBand extends A_ListenerPopupDisplayData{

	public ListenerPopupBand(BandControl controler, A_PopupDisplayData popupDisplayData){
		super(controler, popupDisplayData);
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		boolean leftToRight;
		if(e.getX()>xStart){
			xEnd = e.getX();
			if(xEnd<0)xEnd=0;
			else if(xEnd>popupDisplayData.getWidth())xEnd = popupDisplayData.getWidth();
			leftToRight=true;
		}
		else{
			xStart = e.getX();
			if(xStart<0)xStart=0;
			else if(xStart>popupDisplayData.getWidth())xStart = popupDisplayData.getWidth();
			leftToRight=false;
		}
		value = popupDisplayData.getHeight()-e.getY();
		if(value<0)value=0;
		else if(value>popupDisplayData.getHeight())value = popupDisplayData.getHeight();

		int xMin, xMax;
		xMin = popupDisplayData.convertXFromRefToValue(xStart);
		xMax = popupDisplayData.convertXFromRefToValue(xEnd);
		if(xMax>xMin){
			ValueAndString yValue = popupDisplayData.convertYFromRefToValueForValueAndString(value);
			controler.getDataSet().addRange2Map(new Range(xMin,xMax,controler),yValue);
			if(leftToRight)xStart = xEnd;
			else xEnd=xStart;
			popupDisplayData.repaint();
			controler.getDisplayData().repaint();
		}
	}

}
